package com.anhuanjia.tools.eao.controller;

import org.springframework.util.StringUtils;

import com.anhuanjia.tools.eao.bean.CrmCompany;
import com.anhuanjia.tools.eao.bean.EaoMemberLife;
import com.anhuanjia.tools.eao.beanvo.EaoMemberVo;
import com.efastsoft.bean.EcMember;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;

/**
 * 后台账号操作
 * 
 * @author Administrator
 *
 */
@Alias(value = "accountNumber")
public class AccountNumberUI extends UIFacade {
	/**
	 * 根据id查询账号
	 */
	@Priv(loginType = LoginType.Member)
	public void findOneAccount() {
		String id = $V("id");
		if(StringUtils.isEmpty(id)) {
			f("参数传入失败");
		}
		EaoMemberVo memberVo = new EaoMemberVo();
		DataTable dt = new Q("select * from Ec_Member where id = ? ", id).fetch();
		if (dt.getRowCount() > 0) {
			EcMember member = new EcMember();
			member.setValue(dt.get(0));
			memberVo.setId(member.getID());// id
			memberVo.setUserName(member.getUserName());
			memberVo.setPasswore(member.getPassword());
			DataTable Table = new Q("select memberId , life , createTime from eao_member_life where memberId = ? ",
					member.getID()).fetch();
			EaoMemberLife memberLife = new EaoMemberLife();
			if (Table.getRowCount() != 0) {
				memberLife.setValue(Table.get(0));
				// 使用年限
				memberVo.setCreate_time(memberLife.getCreateTime().toString());
				memberVo.setLife(memberLife.getLife().toString());
			}
			DataTable dtTwo = new Q("select * from Crm_Company where id = ? ", member.getID()).fetch();
			CrmCompany crmCompany = new CrmCompany();
			crmCompany.setValue(dtTwo.get(0));
			memberVo.setFullName(crmCompany.getFullName());
			memberVo.setContactName(crmCompany.getContactName());
			memberVo.setContactPhone(crmCompany.getContactPhone());
			memberVo.setAddress(crmCompany.getAddress());
			memberVo.setLegalRepresentative(crmCompany.getLegalRepresentative());
			DataTable dz = new Q("select Company , count(*) from Mh_Resource where Company = ? ", member.getID())
					.fetch();
			String count = dz.get(0).get(1).toString();
			memberVo.setCount(count);
			this.Response.put("member", memberVo);
		} else {
			f("查询的账号不存在");
		}
	}


	/**
	 * 用户权限
	 */
	@Priv(loginType = LoginType.Member)
	public void userOrg() {
		Long memberId = Member.getUID();
//		获取当前登录账号状态（type）
		String strin = new Q("select type from Ec_Member where ID = ?", memberId.toString()).executeString();
//		获取当前登录账号在人员表的信息
		DataTable dataTable = new Q(" select * from Mh_Resource where Member = ? " , memberId).fetch();
//		判断当前登录账号为（一级/二级）
		if ("Ent".equals(strin) && dataTable.getRowCount() == 0) {
//			一级账号
			this.Response.put("type", "Ent");
		} else {
			String str = new Q("select IsEngineer from eao_is_engineer where Member = ?", memberId).executeString();
			if (StringUtils.isEmpty(str)) {
//				游客账号
				this.Response.put("type", "0");
			} else {
				DataTable table = new Q("select parentId from eao_Position where id = ?", str).fetch();
				for (DataRow dataRow : table) {
					if (StringUtils.isEmpty(dataRow.get(0))) {
						if ("1".equals(str) || "2".equals(str)) {
//						总经理、总工账号
							this.Response.put("type", "12");
						} else if ("3".equals(str)) {
//							分公司负责人账号
							this.Response.put("type", "3");
						} else if ("4".equals(str) || "5".equals(str)) {
//							工程师账号
							this.Response.put("type", "45");
						} else if ("6".equals(str)) {
//							技术员账号
							this.Response.put("type", "6");
						} else if ("7".equals(str)) {
//							档案员账号
							this.Response.put("type", "7");
						} else if ("8".equals(str)) {
//							业务员账号
							this.Response.put("type", "8");
						}
					} else {
						if ("1".equals(table.getString(0, 0)) || "2".equals(table.getString(0, 0))) {
							this.Response.put("type", "12");
						} else if ("3".equals(table.getString(0, 0))) {
							this.Response.put("type", "3");
						} else if ("4".equals(table.getString(0, 0)) || "5".equals(table.getString(0, 0))) {
							this.Response.put("type", "45");
						} else if ("6".equals(table.getString(0, 0))) {
							this.Response.put("type", "6");
						} else if ("7".equals(table.getString(0, 0))) {
							this.Response.put("type", "7");
						} else if ("8".equals(table.getString(0, 0))) {
							this.Response.put("type", "8");
						}
					}
				}
			}
		}
	}

}
